﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Data;
using Kinban.Contracts;

namespace Kinban
{
    public class KanbanQueue : INotifyPropertyChanged
    {
        private ObservableCollection<Story> StoryList;
        private CollectionViewSource _stories;
        private string _queueStatus;
        private readonly List<string> _canTransitionStates;

        public KanbanQueue(string queueStatus, List<string> canTransitionStates)
        {
            _queueStatus = queueStatus;
            _canTransitionStates = canTransitionStates;
            StoryList = new ObservableCollection<Story>();

            _stories = new CollectionViewSource();
            _stories.SortDescriptions.Add(new SortDescription("StackRank", ListSortDirection.Ascending));
            _stories.Source = StoryList;

            StoryList.CollectionChanged += Stories_CollectionChanged;
        }

        void Stories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Story item = GetSignificantItem(e);
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                item.State = _queueStatus;
            }

            string description = null;
            if (item != null)
            {
                description = item.Description;
            }

            Debug.WriteLine("In {2}: {0}, {1}", e.Action, description, _queueStatus);
        }

        internal bool CanAcceptState(string transitionToState)
        {
            return _canTransitionStates.Contains(transitionToState);
        }

        private static Story GetSignificantItem(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Story target = null;
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                var stories = e.NewItems.Cast<Story>();
                target = stories.First();
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                var stories = e.OldItems.Cast<Story>();
                target = stories.First();
            }
            return target;
        }

        internal void SetStories(IEnumerable<Story> stories)
        {
            StoryList.Clear();
            stories.ToList().ForEach(wi => StoryList.Add(wi));
            StoriesView.Refresh();
        }

        public ICollectionView StoriesView { get { return _stories.View; } }
        public ObservableCollection<Story> Stories { get { return StoryList; } }

            #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
